/**
 * @filename:VswaveController Jan 8, 2024
 * @project Wonder Vue Admin Api  1.1.0
 * Copyright(c) 2020 Wonder Co. Ltd. 
 * All right reserved. 
 */
package com.wonder.app.controller;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.vtuzx.core.bean.dto.VtuzxMap;
import com.wonder.admin.domain.SysAdmin;
import com.wonder.admin.service.SysAdminService;
import com.wonder.app.domain.Vswave;
import com.wonder.app.domain.Vswavest;
import com.wonder.app.domain.Wendu;
import com.wonder.app.mapper.VswaveDao;
import com.wonder.app.mapper.VswavestDao;
import com.wonder.app.service.VswaveService;
import com.wonder.app.request.VswaveRequest;
import com.wonder.app.service.VswavestService;
import com.wonder.app.service.WorkSettingService;
import com.wonder.framework.authorization.CheckAdminToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wonder.common.common.AjaxMap;
import com.wonder.common.common.AjaxResult;
import com.wonder.common.common.CommonPage;
import com.wonder.common.utils.MessageUtils;
import com.wonder.common.utils.StringUtils;
import com.alibaba.fastjson.JSON;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;

import javax.servlet.http.HttpServletRequest;

/**
 * 
 * <p>说明： VS波形设置API接口层</P>
 * @version: 1.1.0
 * @author: Wonder
 * @time    Jan 8, 2024
 *
 */
@Api(tags = "VS波形设置")
@RestController
@RequestMapping("/app/Vswave")
public class VswaveController {
	
	@Autowired
	private VswaveService vswaveService;
	@Autowired
	private VswaveDao vswaveDao;
	@Autowired
	private CheckAdminToken checkAdminToken;
	@Autowired
	private SysAdminService adminService;
	@Autowired
	private VswavestService vswavestService;
	@Autowired
	private VswavestDao vswavestDao;
	
	@ApiOperation(value = "VS波形设置列表")
	@RequestMapping(value = "/index", method = RequestMethod.POST)
	public Map<String,Object> index(@RequestBody VswaveRequest vswaveRequest) {
		Page<Vswave> page = new Page<Vswave>(vswaveRequest.getPage(), vswaveRequest.getLimit());
		IPage<Vswave> list = vswaveService.selectList(vswaveRequest, page);
		
		Map<String,Object> retMap=new HashMap<>();
		retMap.put("data",CommonPage.restPage(list));
		
  
		return AjaxMap.success(retMap);
	}
	
	/**
	 * @explain 修改排序开关
	 * @param   entity
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/updateExt")
	@ApiOperation(value = "修改排序开关", notes = "作者：Wonder")
	public AjaxResult<Vswave> updateExt(@RequestBody Vswave entity){
		if (null!=entity) {
			boolean rsg = vswaveService.updateById(entity);
			if (rsg) {
				  return AjaxResult.success("system.updateSuccess");
			  }else {
				  return AjaxResult.failed("system.updateFailed");
			  }
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}
	
	/**
	 * @explain 添加
	 * @param
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/add")
	@ApiOperation(value = "添加", notes = "作者：Wonder")
	public AjaxResult<Boolean> add(HttpServletRequest request, @RequestBody Map<String,Object> map)throws Exception {

		//获取token
		String token = checkAdminToken.getTokenFormRequest(request);
		SysAdmin user = adminService.getAdminByToken(token);
		String userName = user.getName();
		if(map==null)return AjaxResult.failed("system.parameterError");
		List<Map<String,Object>> dataList = (List<Map<String,Object>>)map.get("dataList");
		Map<String,Object> header = (Map<String, Object>) map.get("header");
		boolean isSuccess = vswaveService.savevswave(userName,header,dataList);
		return AjaxResult.success(isSuccess);

	}

	
	/**
	 * @explain 查询对象  <swagger GET请求>
	 * @param   map 对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@PostMapping("/getUpdateInfo")
	@ApiOperation(value = "获取VS波形设置对象", notes = "作者：Wonder")
	public AjaxResult<Vswave> getUpdateInfo(@RequestBody Map<String,Object> map){
	    if(map==null) return AjaxResult.failed("system.parameterError");
		Long id=Long.parseLong(map.get("id").toString());		
		Vswave vswave=vswaveService.getById(id);
//		if(vswave.getFpicture_table()!=null)
//		    vswave.setFpicture(JSON.parseObject(vswave.getFpicture_table()
//		    ,new ArrayList<Map<String, Object>>().getClass()));
//
		if (null!=vswave ) {
			 return AjaxResult.success(vswave);
		}else {
			 return AjaxResult.failed("system.parameterError");
		}
		
	}

	/**
	 * 修改
	 * @param request
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/update")
	@ApiOperation(value = "修改", notes = "作者：Wonder")
	public AjaxResult<Boolean> update(HttpServletRequest request, @RequestBody Map<String,Object> map)throws Exception {

		//获取token
		String token = checkAdminToken.getTokenFormRequest(request);
		SysAdmin user = adminService.getAdminByToken(token);
		String userName = user.getName();
		if(map==null)return AjaxResult.failed("system.parameterError");
		List<Map<String,Object>> dataList = (List<Map<String,Object>>)map.get("dataList");
		Map<String,Object> header = (Map<String, Object>) map.get("header");
		boolean isSuccess = vswaveService.updatewave(userName,header,dataList);
		return AjaxResult.success(isSuccess);

	}
	
	/**
	 * @explain 删除
	 * @param   map 对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@PostMapping("/delete")
	@ApiOperation(value = "删除", notes = "作者：Wonder")
	public AjaxResult<Vswave> delete(@RequestBody Map<String,Object> map){
		if(map==null)return AjaxResult.failed("system.parameterError");
		String ids=map.get("id").toString();
		ids=StringUtils.remove(ids, '[');
		ids=StringUtils.remove(ids, ']');
		String[] deleteIds=StringUtils.split(ids, ',');
		List<Long> idList=new ArrayList<>(); 
		for(int i=0;i<deleteIds.length;i++)
		{
			idList.add( Long.parseLong(deleteIds[i].trim()));//整数数组
		}

	    boolean rsg = vswaveService.removeBatchByIds(idList);
		boolean detailrsg = vswavestService.remove(new QueryWrapper<Vswavest>().eq("detailid",ids));
	    if (rsg) {
		    return AjaxResult.success("system.deleteSuccess");
	    }else {
		   return AjaxResult.failed("system.deleteFailed");
	    }
		
	}

	/**
	 * 查看测试波形明细展示页面
	 * @param map
	 * @return
	 */
	@PostMapping("/detail")
	@ApiOperation(value = "获取VS波形明细", notes = "作者：Wonder")
	public AjaxResult<VtuzxMap> detail(@RequestBody Map<String,Object> map){
		if(map==null) return AjaxResult.failed("system.parameterError");
		Long id = Long.parseLong(map.get("id").toString());
		VtuzxMap data = vswaveService.getDetail(id);
		if (null!=data ) {
			return AjaxResult.success(data);
		}else {
			return AjaxResult.failed("system.parameterError");
		}

	}

	/**
	 * 审核单据
	 * @param request
	 * @param entity
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/audit")
	@ApiOperation(value = "审核", notes = "作者：Wonder")
	public AjaxResult<Vswave> audit(HttpServletRequest request,@RequestBody Vswave entity)throws Exception{
		if (null!=entity) {
//			if(entity.getFpicture()!=null)
//				entity.setFpicture_table(JSON.toJSONString(entity.getFpicture()));
			String validate=vswaveService.validate(entity,"update");
			if(StringUtils.hasText(validate))
				return AjaxResult.failed(validate);
			//获取token
			String token = checkAdminToken.getTokenFormRequest(request);
			SysAdmin user = adminService.getAdminByToken(token);
			//获取登录名 并赋值
			String userName = user.getName();
			entity.setFaudituser(userName);

			//获取当前日期
			Date currentDate = new Date();
			//审核日期
			entity.setFauditdate(currentDate);
			entity.setFstatus(1);
			boolean rsg = vswaveService.updateById(entity);
			if (rsg) {
				return AjaxResult.success("system.updateSuccess");
			}else {
				return AjaxResult.failed("system.updateFailed");
			}
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}

	/**
	 * 反审核单据
	 * @param request
	 * @param entity
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/unaudit")
	@ApiOperation(value = "反审核", notes = "作者：Wonder")
	public AjaxResult<Vswave> unaudit(HttpServletRequest request,@RequestBody Vswave entity)throws Exception{
		if (null!=entity) {
//			if(entity.getFpicture()!=null)
//				entity.setFpicture_table(JSON.toJSONString(entity.getFpicture()));
			String validate=vswaveService.validate(entity,"update");
			if(StringUtils.hasText(validate))
				return AjaxResult.failed(validate);

			entity.setFaudituser("");

			//审核日期
			entity.setFauditdate(null);
			entity.setFstatus(0);
			boolean rsg = vswaveService.updateById(entity);
			if (rsg) {
				return AjaxResult.success("system.updateSuccess");
			}else {
				return AjaxResult.failed("system.updateFailed");
			}
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}

	/**
	 * 获取VS波形信息
	 * @return
	 */
	@ApiOperation(value = "获取Vs波形列表", notes = "作者：mjy")
	@RequestMapping(value = "/get_vslist", method = RequestMethod.POST)
	public AjaxResult<List<VtuzxMap>> getvswavelist () {
		List<Vswave> vswaveList = vswaveService.list();
		List<VtuzxMap> dataList = new ArrayList<>();
		if (null != vswaveList) {
			for (Vswave vswave : vswaveList){
				String vsName = vswave.getFname();
				String vsNumber = vswave.getFnumber();
				long vsid = vswave.getId();
				//查询明细信息并添加
				List<Vswavest> vswavestList = vswavestDao.selectList(new QueryWrapper<Vswavest>().eq("detailid",vsid));
				for (Vswavest vswavest : vswavestList){
					Long id = vswavest.getId();
					double wendu =vswavest.getFemperature();
					String boxing = vswavest.getFboxing();
					String dianya = vswavest.getFdianya();
					double fngtype = vswavest.getFngtype();
					double Fresidtime = vswavest.getFresidtime();
					double Frefreshtime = vswavest.getFrefreshtime();
					long ftuopanqty = vswavest.getFtuopanqty();
					int fpictureId = vswave.getFpictureId();
					VtuzxMap data =new VtuzxMap();
					data.append("vsName",vsName)
							.append("vsNumber",vsNumber)
							.append("id",id)
							.append("vsid",vsid)
							.append("wendu",wendu)
							.append("boxing",boxing)
							.append("dianya",dianya)
							.append("fngtype",fngtype)
							.append("fresidtime",Fresidtime)
							.append("frefreshtime",Frefreshtime)
							.append("fpictureId",fpictureId)
							.append("ftuopanqty",ftuopanqty);
					dataList.add(data);
				}
			}
			return AjaxResult.success(dataList);
		} else {
			return AjaxResult.failed("system.parameterError");
		}
	}
}